package com.ftg.learn.chapter04.string;

/**
 * @author 布衣
 */
public class T {

//    杨欣建
    public static int searchWithIndex(String a, String b) {
        for (int i = 0; i < a.length(); i++) {
            boolean ist = true;
            for (int j = 0; j < b.length(); j++) {
                if (a.charAt(i) != b.charAt(j)) {
                    ist =false;
                    break;
                }
            }
            if (ist) {
                return i;
            }
        }
        return -1;
    }


    // 李宇哲
    public int searchWithIndex3(String s, String c) {
        //临界
        for (int i = 0; i < s.length() - c.length(); i++) {
            if (s.charAt(i) == c.charAt(0)) {
                if (s.substring(i, i + c.length()).equals(c)) {
                    return i;
                }
            }
        }
        return 1;
    }

    //梁宇轩
    public int searchWithIndex2(String s, String p) {
        assert s != null && p != null;
        for (int i = 0; i <= s.length() - p.length(); i++) {
            if (s.startsWith(p, i)) {
                return i;
            }
        }
        return -1;
    }


    //张旭
    public int searchWithIndex1(String s, String p) {

        assert s != null;
        assert p != null;


        //临界处理
        for (int i = 0; i < s.length() - p.length(); i++) {
            for (int j = 0; j < p.length(); j++) {
                if (s.charAt(i + j) != p.charAt(j)) {
                    break;
                }
                if (j == p.length() - 1) {
                    return i;
                }
            }
        }

        return -1;
    }

    public static void main(String[] args) {
        T t1 = new T();
        int i = t1.searchWithIndex("ababacdef", null);
        System.out.println(i);
    }

}
